<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Index extends Controller_Index_Base
{
    public function action_index()
    {           
        $categories = ORM::factory('category')->get_main_categories();
        $products   = ORM::factory('product')->get_products_for_main();
        $categories_tree = ORM::factory('category')->get_categories_tree(); 
        
        $content = new View('index/index/index_tpl');
        $content->categories = $categories;
        $content->products   = $products;    
        $content->categories_tree = $categories_tree;
        $all_products = array();
        foreach ($categories as $category) {
            $all_products = array_merge($all_products, $products[$category->id]);    
        }
        $content->coupons = ORM::factory('coupon')->get_coupons_by_products($all_products);
        
        $this->print_template('Обувь и одежда со скидками', $content);
    }
    
    public function action_search()
    {
        $query    = strip_tags($this->request->param('query'));
        $query    = Model_Product::get_clean_search_query($query);
        $page     = $this->request->param('page') ? $this->request->param('page') : 1;
               
        $result   = ORM::factory('product')->search($query, $page);
        $products = $result['products'];
        $count    = $result['count']; 
        /*if (!count($products)) {
            $session = Session::instance();
            if ($category_id = $session->get('category-redirect')) {
                $category = ORM::factory('category')->filter($category_id);
                $session->set('category-redirect', false);
                Helper_Index::redirect_301(Helper_Index::category_url($category));    
            }    
        }  */
        $content  = new View('index/index/search_tpl'); 
        $content->query    = $query;
        $content->products = $products; 
        $content->coupons  = ORM::factory('coupon')->get_coupons_by_products($products); 
        $content->categories = ORM::factory('category')->get_categories_by_products($products); 
           
        $content->pagination = array(                                                   
            'page' => $page,
            'per_page' => Model_Product::get_products_per_page(),
            'count' => $count,
        );
            
        $this->print_template(Model_Main::ucfirst_utf8($query).' - Поиск', $content, null, null, $query);
    }
    
    public function action_search_ajax()
    {
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $query  = trim(strip_tags($this->request->post('query')));
            if ($query) {
                $models = ORM::factory('model')->search($query, 15);              
                $result = array(
                    'success' => true,
                    'data'    => Helper_Index::models_search_list($models)->render(),
                );
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_offer()
    {
        if (HTTP_Request::POST == $this->request->method()) {
            $email    = $this->request->post('email');
            //$password = $this->request->post('password');
            $url      = $this->request->post('url');
                  
            try {
                $password = ORM::factory('user')->generate_password();
                
                $user = ORM::factory('user')->add_user($email, $password);
                $shop = ORM::factory('shop')->add_shop($url);                                
                $_POST = array();
                 
                $user->add_to_shop($shop); 
                Model_Auth::instance()->login($email, $password, true);
                  
                $shop->set_as_current();                     
                Request::current()->redirect(Helper_Admin::get_url() . '/index/start'); 
            } catch (Exception $e) {
                $message = 'Форма заполнена с ошибками!';
                if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                    $message = 'Данный e-mail или магазин уже зарегистрированы';
                }
            }
        }
                
        $content = new View('index/index/offer_tpl');        
        $this->response->body($content);
    }
} 
